package com.twu.biblioteca.Models;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * User: spacetime
 * Date: 8/9/13
 * Time: 7:37 AM
 */
public class Menu {
    private ArrayList<String> menuItems = new ArrayList<String>();
    private User loggedInUser;

    public Menu() {
        menuItems.add("1. View All Books");
        menuItems.add("2. Reserve a Book");
        menuItems.add("3. Check membership details");
        menuItems.add("4. Show Movie Library");
        menuItems.add("5. Log In");
    }

    public String selectOption(BookLibrary bookLibrary, MovieLibrary movieLibrary,
                               UserList userList, User loggedInUser, int selection) {
        if (selection - 1 < this.menuItems.size() && selection > 0) {
            switch (selection){
                case 1:
                    this.viewAllBooks(bookLibrary);
                    break;
                case 2:
                    this.reserveBook(bookLibrary, loggedInUser);
                    break;
                case 3:
                    this.checkMembership(bookLibrary);
                    break;
                case 4:
                    this.viewAllMovies(movieLibrary);
                    break;
                case 5:
                    this.loggedInUser = this.attemptLogIn(userList);
                    loggedInUser = this.loggedInUser;
                    break;
            }
            return "\n";
        }
        return "Select a valid option!!";
    }

    private User attemptLogIn(UserList userList) {
        System.out.print("Enter username and password: ");

        Scanner optionScanner = new Scanner(System.in);
        String username = optionScanner.nextLine();
        String password = optionScanner.nextLine();
        // System.out.print(username + password);
        User authenticatedUser = userList.getUserWithUsername(username);
        if (authenticatedUser == null || !authenticatedUser.authenticate(username, password))
            System.out.print("Wrong Credentials. Login failed.\n");
        else {
            System.out.print("Login Successful.\n");
            return authenticatedUser;
        }
        return null;
    }

    private void viewAllMovies(MovieLibrary movieLibrary) {
        System.out.print(movieLibrary.getCompleteListing());
    }

    private void checkMembership(BookLibrary bookLibrary) {
        if (loggedInUser == null)
            System.out.print("Please talk to a Librarian. Thank you.\n");
        else {
            System.out.print("Username: " + loggedInUser.getUsername());
            System.out.print("\nEmail ID: " + loggedInUser.getEmailID());
            System.out.print("\nPhone: " + loggedInUser.getPhoneNumber());
        }
    }

    private void reserveBook(BookLibrary bookLibrary, User loggedInUser) {
        if (loggedInUser == null) {
            System.out.print("Please talk to a Librarian or login. Thank you.\n");
            return;
        }
        Scanner bookIDScanner = new Scanner(System.in);
        System.out.print("Enter the ID of the book: ");
        int bookID = bookIDScanner.nextInt();

        Book retrievedBook = bookLibrary.getBookWithID(bookID);

        if (retrievedBook != null && !retrievedBook.isReserved()) {
            System.out.print("Thank You! Enjoy the book.\n");
            retrievedBook.setAsReserved();
        }
        else {
            System.out.print("Sorry we don't have that book yet.\n");
        }
    }

    private void viewAllBooks(BookLibrary bookLibrary) {
        System.out.print("List of All Available Books:\n");
        System.out.print(bookLibrary.getStringOfAllBooks());
    }

    public void printSelectionMenu() {
        System.out.print("\n\n*************\nSelection Menu\n");
        for (String menuItem : menuItems)
            System.out.println(menuItem);
    }

    public void acceptMenuSelectionInputAndExecuteOption(BookLibrary bookLibrary, MovieLibrary movieLibrary,
                                                         UserList userList, User loggedInUser) {
        System.out.println("Enter Choice: ");
        Scanner optionScanner = new Scanner(System.in);
        int choice = optionScanner.nextInt();

        this.selectOption(bookLibrary, movieLibrary, userList, loggedInUser, choice);
    }
}
